from domain.domain import *
from service.Message import Message,CodeMessageData


class DataChange():
    """
    data change
    """
    def __init__(self):
        print("DataChange init")


    def userDataChange(self,userData,idFlag=0,changeType=0):
        """
        can chang web data
        :param userData: user data
        :param idFlag: judge has or has not id
        :param changeType: changeType is true  userData is list . And changeType is false userData is object
        :return:
        """
        newUser=User()
        data =[]
        code = 0000
        try:
            #使用列表输入
            if(changeType):
                for each in userData:
                    part = User()
                    print(each)
                    if (idFlag):
                        part.id = each['id']
                    part.job_number = str(int(each['job_number']))
                    part.user_name = each['user_name']
                    part.password = each['password']
                    part.access_level = int(each["access_level"])
                    data.append(part)
            else:
               part = User()
               part.job_number = userData['job_number']
               part.user_name = userData['user_name']
               part.password = userData['password']
               part.access_level = userData["access_level"]
               data.append(part)
        except:
            code=2003
        return Message(code,CodeMessageData[code],data)





    def bookDataChange(self,bookData,idFlag=0,jobNumberJudge=[]):
        """
        can change web data
        :param bookData:
        :param idFlag: judge id
        :param  jobNumberJudge : job number judge flag
        :return:
        """
        data = []
        code = 0000
        judge=len(jobNumberJudge)
        try:
            for each in bookData:
                part = Book()
                if(idFlag):
                    part.id=each['id']
                if (judge == 0):
                    part.job_number = str(int(each['job_number']))
                else:
                    part.job_number = jobNumberJudge[0]
                part.author_is_eic = each['author_is_eic']
                part.isbn_number = each['isbn_number']
                part.own_words = each['own_words']
                part.press_count = each['press_count']
                part.press_level = each['press_level']
                part.press_name = each['press_name']
                part.press_time = each['press_time']
                part.result_form = str(int(each['result_form']))
                part.result_name = each['result_name']
                part.total_words = each['total_words']
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)






    def exchangeDataChange(self,exchangeData,idFlag=0,jobNumberJudge=[]):
        """
        can change web data
        :param exchangeData:
        :return:
        """
        data = []
        code = 0000
        judge=len(jobNumberJudge)
        try:
            for each in exchangeData:
                part = Exchange()
                if(idFlag):
                    part.id = each['id']
                if (judge == 0):
                    part.job_number = each['job_number']
                else:
                    part.job_number = jobNumberJudge[0]
                part.content = each['content']
                part.end_time = each['end_time']
                part.start_time = each['start_time']
                part.teacher_name = each['teacher_name']
                part.theme = each['theme']
                part.train_type = each['train_type']
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)


    def teachingScienceChange(self, teachingScienceData, idFlag=0, jobNumberJudge=[]):
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in teachingScienceData:
                part = TeachingScience()
                if(idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number = jobNumberJudge[0]
                part.result_form = each['result_form']
                part.result_name=each['result_name']
                part.units_issued = each['units_issued']
                part.award_type = each['award_type']
                part.award_grade = each['award_grade']
                part.award_year = each['award_year']
                part.order = each['order']
                part.total_people = each['total_people']

                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)

    def paperDataChange(self,paperData,idFlag=0,jobNumberJudge=[]):
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in paperData:
                part = Paper()
                if(idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number = jobNumberJudge[0]
                part.result_name = each['result_name']
                part.result_form = each['result_form']
                part.publication_name = each['publication_name']
                part.issuing_time = each['issuing_time']
                part.isbn_number = each['isbn_number']
                part.publication_level = each['publication_level']
                part.search_info = each['search_info']
                part.order = each['order']
                part.total_people = each['total_people']
                
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)

    def patentDataChange(self,patentData,idFlag=0,jobNumberJudge=[]):
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in patentData:
                part = Patent()
                if(idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number=jobNumberJudge[0]
                part.result_name = each['result_name']
                part.result_form = each['result_form']
                part.units_issued = each['units_issued']
                part.patent_type = each['patent_type']
                part.patent_number = each['patent_number']
                part.patent_year = each['patent_year']
                part.award_time = each['award_time']
                part.order = each['order']
                part.total_people = each['total_people']
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)

    def personalTeamDataChange(self, personalTeamData, idFlag=0,jobNumberJudge=[]):
        """
        can change web data
        :param personalTeamData: personalTeam data
        :param idFlag: judge id
        :return:
        """
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in personalTeamData:
                part = PersonalTeam()
                if (idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number =jobNumberJudge[0]
                part.title = each['title']
                part.honor_type = each['honor_type']
                part.name = each['name']
                part.time = each['time']
                part.rewards_bureau = each['rewards_bureau']
                part.theme = each['theme']
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)

    def projectDataChange(self,projectData,idFlag=0,jobNumberJudge=[]):
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in projectData:
                part = Project()
                if (idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number=jobNumberJudge[0]
                # if(each['presenter_flag']=='1'):
                #     part.presenter_flag =True
                # else:
                #     part.presenter_flag =False
                part.presenter_flag=int(each['presenter_flag'])
                part.fee = each['fee']
                part.member = each['member']
                part.order = each['order']

                part.project_end_time = each['project_end_time']
                part.project_name = each['project_name']
                part.project_number = each['project_number']
                part.project_start_time = each['project_start_time']
                part.project_type = each['project_type']
                part.remark = each['remark']
                part.support_unit = each['support_unit']
                
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)


    def subjectDataChange(self,subjectData,idFlag=0,jobNumberJudge=[]):
        data = []
        code = 0000
        judge = len(jobNumberJudge)
        try:
            for each in subjectData:
                part = Subject()
                if (idFlag):
                    part.id = each['id']
                if(judge==0):
                    part.job_number = each['job_number']
                else:
                    part.job_number=jobNumberJudge[0]
                part.advisor = each['advisor']
                part.award_level = each['award_level']
                part.certificate_copy = each['certificate_copy']
                part.competition_name = each['competition_name']

                part.number = each['number']
                part.remark = each['remark']
                part.teams = each['teams']
                
                data.append(part)
        except:
            code = 2004
        return Message(code, CodeMessageData[code], data)

    def fileDataChange(self,fileData):
        data = []
        code = 0000
        try:
            part=File()
            part.job_number=fileData[0]
            part.year=fileData[1]
            part.excel_type=fileData[2]
            part.type_file_id=fileData[3]
            part.file_name=fileData[4]
            data.append(part)
        except:
            code=2004
            data=[]
        return Message(code,CodeMessageData[code],data)


